home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / ruby / 1.8 / jcode.rb < prev    next >
Encoding:
Ruby Source  |  2007-03-07  |  4.3 KB  |  222 lines

  1. # jcode.rb - ruby code to handle japanese (EUC/SJIS) string
  2.  
  3. if $VERBOSE && $KCODE == "NONE"
  4.   warn "Warning: $KCODE is NONE."
  5. end
  6.  
  7. $vsave, $VERBOSE = $VERBOSE, false
  8. class String
  9.   warn "feel free for some warnings:\n" if $VERBOSE
  10.  
  11.   def _regex_quote(str)
  12.     str.gsub(/(\\[\[\]\-\\])|\\(.)|([\[\]\\])/) do
  13.       $1 || $2 || '\\' + $3
  14.     end
  15.   end
  16.   private :_regex_quote
  17.  
  18.   PATTERN_SJIS = '[\x81-\x9f\xe0-\xef][\x40-\x7e\x80-\xfc]'
  19.   PATTERN_EUC = '[\xa1-\xfe][\xa1-\xfe]'
  20.   PATTERN_UTF8 = '[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf][\x80-\xbf]'
  21.  
  22.   RE_SJIS = Regexp.new(PATTERN_SJIS, 0, 'n')
  23.   RE_EUC = Regexp.new(PATTERN_EUC, 0, 'n')
  24.   RE_UTF8 = Regexp.new(PATTERN_UTF8, 0, 'n')
  25.  
  26.   SUCC = {}
  27.   SUCC['n'] = Hash.new(1)
  28.   SUCC['s'] = Hash.new(1)
  29.   for i in 0 .. 0x3f
  30.     SUCC['s'][i.chr] = 0x40 - i
  31.   end
  32.   SUCC['s']["\x7e"] = 0x80 - 0x7e
  33.   SUCC['s']["\xfd"] = 0x100 - 0xfd
  34.   SUCC['s']["\xfe"] = 0x100 - 0xfe
  35.   SUCC['s']["\xff"] = 0x100 - 0xff
  36.   SUCC['e'] = Hash.new(1)
  37.   for i in 0 .. 0xa0
  38.     SUCC['e'][i.chr] = 0xa1 - i
  39.   end
  40.   SUCC['e']["\xfe"] = 2
  41.   SUCC['u'] = Hash.new(1)
  42.   for i in 0 .. 0x7f
  43.     SUCC['u'][i.chr] = 0x80 - i
  44.   end
  45.   SUCC['u']["\xbf"] = 0x100 - 0xbf
  46.  
  47.   def mbchar?
  48.     case $KCODE[0]
  49.     when ?s, ?S
  50.       self =~ RE_SJIS
  51.     when ?e, ?E
  52.       self =~ RE_EUC
  53.     when ?u, ?U
  54.       self =~ RE_UTF8
  55.     else
  56.       nil
  57.     end
  58.   end
  59.  
  60.   def end_regexp
  61.     case $KCODE[0]
  62.     when ?s, ?S
  63.       /#{PATTERN_SJIS}$/on
  64.     when ?e, ?E
  65.       /#{PATTERN_EUC}$/on
  66.     when ?u, ?U
  67.       /#{PATTERN_UTF8}$/on
  68.     else
  69.       /.$/on
  70.     end
  71.   end
  72.  
  73.   alias original_succ! succ!
  74.   private :original_succ!
  75.  
  76.   alias original_succ succ
  77.   private :original_succ
  78.  
  79.   def succ!
  80.     reg = end_regexp
  81.     if self =~ reg
  82.       succ_table = SUCC[$KCODE[0,1].downcase]
  83.       begin
  84.     self[-1] += succ_table[self[-1]]
  85.     self[-2] += 1 if self[-1] == 0
  86.       end while self !~ reg
  87.       self
  88.     else
  89.       original_succ!
  90.     end
  91.   end
  92.  
  93.   def succ
  94.     str = self.dup
  95.     str.succ! or str
  96.   end
  97.  
  98.   private
  99.  
  100.   def _expand_ch str
  101.     a = []
  102.     str.scan(/(?:\\(.)|([^\\]))-(?:\\(.)|([^\\]))|(?:\\(.)|(.))/m) do
  103.       from = $1 || $2
  104.       to = $3 || $4
  105.       one = $5 || $6
  106.       if one
  107.     a.push one
  108.       elsif from.length != to.length
  109.     next
  110.       elsif from.length == 1
  111.     from[0].upto(to[0]) { |c| a.push c.chr }
  112.       else
  113.     from.upto(to) { |c| a.push c }
  114.       end
  115.     end
  116.     a
  117.   end
  118.  
  119.   def expand_ch_hash from, to
  120.     h = {}
  121.     afrom = _expand_ch(from)
  122.     ato = _expand_ch(to)
  123.     afrom.each_with_index do |x,i| h[x] = ato[i] || ato[-1] end
  124.     h
  125.   end
  126.  
  127.   HashCache = {}
  128.   TrPatternCache = {}
  129.   DeletePatternCache = {}
  130.   SqueezePatternCache = {}
  131.  
  132.   public
  133.  
  134.   def tr!(from, to)
  135.     return nil if from == ""
  136.     return self.delete!(from) if to == ""
  137.  
  138.     pattern = TrPatternCache[from] ||= /[#{_regex_quote(from)}]/
  139.     if from[0] == ?^
  140.       last = /.$/.match(to)[0]
  141.       self.gsub!(pattern, last)
  142.     else
  143.       h = HashCache[from + "1-0" + to] ||= expand_ch_hash(from, to)
  144.       self.gsub!(pattern) do |c| h[c] end
  145.     end
  146.   end
  147.  
  148.   def tr(from, to)
  149.     (str = self.dup).tr!(from, to) or str
  150.   end
  151.  
  152.   def delete!(del)
  153.     return nil if del == ""
  154.     self.gsub!(DeletePatternCache[del] ||= /[#{_regex_quote(del)}]+/, '')
  155.   end
  156.  
  157.   def delete(del)
  158.     (str = self.dup).delete!(del) or str
  159.   end
  160.  
  161.   def squeeze!(del=nil)
  162.     return nil if del == ""
  163.     pattern =
  164.       if del
  165.     SqueezePatternCache[del] ||= /([#{_regex_quote(del)}])\1+/
  166.       else
  167.     /(.|\n)\1+/
  168.       end
  169.     self.gsub!(pattern, '\1')
  170.   end
  171.  
  172.   def squeeze(del=nil)
  173.     (str = self.dup).squeeze!(del) or str
  174.   end
  175.  
  176.   def tr_s!(from, to)
  177.     return self.delete!(from) if to.length == 0
  178.  
  179.     pattern = SqueezePatternCache[from] ||= /([#{_regex_quote(from)}])\1*/
  180.     if from[0] == ?^
  181.       last = /.$/.match(to)[0]
  182.       self.gsub!(pattern, last)
  183.     else
  184.       h = HashCache[from + "1-0" + to] ||= expand_ch_hash(from, to)
  185.       self.gsub!(pattern) do h[$1] end
  186.     end
  187.   end
  188.  
  189.   def tr_s(from, to)
  190.     (str = self.dup).tr_s!(from,to) or str
  191.   end
  192.  
  193.   def chop!
  194.     self.gsub!(/(?:.|\r?\n)\z/, '')
  195.   end
  196.  
  197.   def chop
  198.     (str = self.dup).chop! or str
  199.   end
  200.  
  201.   def jlength
  202.     self.gsub(/[^\Wa-zA-Z_\d]/, ' ').length
  203.   end
  204.   alias jsize jlength
  205.  
  206.   def jcount(str)
  207.     self.delete("^#{str}").jlength
  208.   end
  209.  
  210.   def each_char
  211.     if block_given?
  212.       scan(/./m) do |x|
  213.         yield x
  214.       end
  215.     else
  216.       scan(/./m)
  217.     end
  218.   end
  219.  
  220. end
  221. $VERBOSE = $vsave
  222.